<?php

namespace App\Http\Controllers\Admin;
use App\Exceptions\ServiceException;
use App\Services\Game\NeigouService;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ExcelExport;
use App\Http\Controllers\Controller;
use App\Utils\ResponseUtil;
use App\DTOS\Admin\VirtualOrderSearchDTO;
use App\Services\VirtualOrderService;
use App\DTOS\Admin\VirtualOrderInsertDTO;
use App\DTOS\Admin\VirtualOrderUpdateDTO;


// 虚拟订单
class VirtualOrderController extends Controller
{
    private VirtualOrderService $service;
    private NeigouService $NeigouService;

    /**
     * VirtualOrderService constructor.
     * @param VirtualOrderService $service
     * @param NeigouService $NeigouService
     */
    public function __construct(VirtualOrderService $service,NeigouService $NeigouService)
    {
        $this->service = $service;
        $this->NeigouService = $NeigouService;
    }

    public function import()
    {
        $file = request()->file('file');
        $sheets = Excel::toArray(null,$file);
        $sheet = $sheets[0];
        $inserts = [];
        foreach($sheet as $key=>$arr) {
            if ($key>1){
                $row = [
                    'column_1'=>$arr[1],
                    'column_2'=>$arr[2],
                ];
                $inserts[] = $row;
            }
        }
    }

    public function export()
    {
        $DTO = new VirtualOrderSearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $excelRows = [
            ['ID','创建时间']
        ];
        foreach($paginate->items() as $datum){
            $excelRows[] = [ $datum['id'], $datum['created_at'] ];
        }
        $excel = new ExcelExport($excelRows);
        return Excel::download($excel, '虚拟订单.xlsx');
    }

    public function paginate()
    {
        $DTO = new VirtualOrderSearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $data = $paginate->items();
        foreach($data as &$datum){
            $datum['createdAt'] = (new \DateTime($datum['created_at']))->format('Y-m-d H:i:s');
            $datum['customer_id'] = (string)$datum['customer_id'];
        }
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

    public function insert()
    {
        try {
            $DTO = new VirtualOrderInsertDTO(request()->post());
            $this->service->insert($DTO);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }
    public function update($id)
    {
        try{
            $DTO = new VirtualOrderUpdateDTO(request()->post());
            $this->service->updateById($id,$DTO);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function delete($id){
        try{
            $this->service->deleteById($id);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }
    public function detail($id)
    {
        $data = $this->service->getById($id);
        return ResponseUtil::success($data);
    }
    public function auth($id)
    {
        try {
            $status = request()->get('status'); //审核状态
            $this->service->auth($id,$status);
            return ResponseUtil::success();
        } catch (ServiceException $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

}
